package com.quanyan.user.service.impl;

import com.google.common.base.Function;
import com.google.common.collect.Maps;
import com.quanyan.api.APIResponse;
import com.quanyan.common.utils.DateUtils;
import com.quanyan.common.utils.StringUtil;
import com.quanyan.redisCluster.utils.RedisKey;
import com.quanyan.user.cache.MyRedisTemplate;
import com.quanyan.user.constants.UserConstants;
import com.quanyan.user.mapper.TbUserFollowMapper;
import com.quanyan.user.mapper.TbUserMapper;
import com.quanyan.user.mapper.UserInfoMapper;
import com.quanyan.user.model.db.TbUser;
import com.quanyan.user.model.db.TbUserExample;
import com.quanyan.user.model.db.TbUserFollow;
import com.quanyan.user.model.db.TbUserFollowExample;
import com.quanyan.user.response.RespUserInfoBase;
import com.quanyan.user.response.RespUserInfoDetail;
import com.quanyan.user.service.UserQueryService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by Administrator on 2016/9/11.
 */
@Service
public class UserQueryServiceImpl implements UserQueryService {

    private static final Logger logger = LoggerFactory.getLogger(UserQueryServiceImpl.class);

    @Autowired
    private TbUserMapper tbUserMapper;

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private MyRedisTemplate myRedisTemplate;

    @Autowired
    private TbUserFollowMapper tbUserFollowMapper;


    //用户默认头像地址
    @Value("${user_default_icon}")
    private String userDefaultIcon;

    /**
     * 手机号方式
     */
    public final byte mobileType = 1;

    /**
     * 微信方式
     */
    public final byte wxType = 2;


    /**
     * 查询用户基本信息
     * @param uid
     * @return
     */
    public APIResponse<RespUserInfoBase> queryUserInfo(Integer uid){
        if(uid == null || uid == 0){
            logger.info("uid不能为空");
            return APIResponse.returnFail("uid 不能为空！");
        }
        TbUser tbUser = this.getUser(uid);
        if(tbUser == null){
            logger.info("用户未注册!");
            return APIResponse.returnFail("用户未注册");
        }
        RespUserInfoBase respUserInfoBase = this.covertRespUserInfoBase(tbUser);
        logger.info("结束查询用户信息：{}",respUserInfoBase.toString());
        return APIResponse.returnSuccess(respUserInfoBase);
    }

    /**
     * 用户查看好友信息
     * @param uid //登录id
     * @param friendUid // 好友id
     * @return
     */
    public APIResponse<RespUserInfoBase> queryUserInfo(Integer uid ,Integer friendUid){
        logger.info("用户查看好友信息-------》");
        if(uid == null){
            logger.info("uid不能为空");
            return APIResponse.returnFail("uid 不能为空！");
        }

        if(friendUid == null){
            logger.info("friendUid不能为空");
            return APIResponse.returnFail("friendUid 不能为空！");
        }

        logger.info("用户{} --》开始查询好友--》{}信息",uid,friendUid);
        TbUser tbUser = this.getUser(friendUid);
        if(tbUser == null){
            logger.info("好友未注册!");
            return APIResponse.returnFail("好友用户未注册");
        }
        RespUserInfoBase respUserInfoBase = this.covertRespUserInfoBase(tbUser);
        /** <<=============== 查询好友备注====================>>**/
        String friendRemark = this.getUserFriendRemark(uid,friendUid);
        respUserInfoBase.setFriendRemarks(friendRemark);
        /** <<=============== 查询好友备注====================>>**/
        logger.info("结束查询用户信息：{}",respUserInfoBase.toString());
        return APIResponse.returnSuccess(respUserInfoBase);
    }

    /**
     * 查询用户详细信息
     * @param uid
     * @return
     */
    public APIResponse<RespUserInfoDetail> queryUserInfoDetail(Integer uid){
        if(uid == null){
            logger.info("uid不能为空");
            return APIResponse.returnFail("uid 不能为空！");
        }
        logger.info("开始查询用户详细信息：{}",uid);
        RespUserInfoDetail respUserInfoDetail;
        try {
            respUserInfoDetail = this.getUserDetail(uid);
        } catch (Exception e) {
            logger.error("{}",e);
            return APIResponse.returnFail("系统异常！");
        }
        if( respUserInfoDetail == null) {
            logger.info("用户未注册!");
            return APIResponse.returnFail("用户未注册");
        }
        logger.info("用户详细信息查询结束{}",respUserInfoDetail.toString());
        return APIResponse.returnSuccess(respUserInfoDetail);
    }


    /**
     * 查看好友详细信息
     * @param uid
     * @param friendUid
     * @return
     */
    public APIResponse<RespUserInfoDetail> queryUserInfoDetail(Integer uid ,Integer friendUid){
        if(uid == null){
            logger.info("uid不能为空");
            return APIResponse.returnFail("uid 不能为空！");
        }

        if(friendUid == null){
            logger.info("friendUid不能为空");
            return APIResponse.returnFail("friendUid 不能为空！");
        }

        logger.info("用户{} --》开始查询好友--》{}详细信息",uid,friendUid);
        RespUserInfoDetail respUserInfoDetail;
        try {
            respUserInfoDetail = this.getUserDetail(uid);
            /** <<=============== 查询好友备注====================>>**/
            String friendRemark = this.getUserFriendRemark(uid,friendUid);
            respUserInfoDetail.setFriendRemarks(friendRemark);
            /** <<=============== 查询好友备注====================>>**/
        } catch (Exception e) {
            logger.error("{}",e);
            return APIResponse.returnFail("系统异常！");
        }
        if( respUserInfoDetail == null) {
            logger.info("用户未注册!");
            return APIResponse.returnFail("用户未注册");
        }
        logger.info("用户详细信息查询结束{}",respUserInfoDetail.toString());
        return APIResponse.returnSuccess(respUserInfoDetail);
    }

    /**
     * 通过手机号查询用户基本信息
     * @param phone
     * @return
     */
    public APIResponse<RespUserInfoBase> queryUserInfo(String phone) {
        if(phone == null){
            logger.info("phone参数不能为空！");
            return APIResponse.returnFail("mobile不能为空！");
        }
        logger.info("开始根据手机号查询用户信息:{}",phone);
        TbUser tbUser = this.getUser(phone, this.mobileType);
        if(tbUser == null){
            logger.info("用户未注册!");
            return APIResponse.returnFail("用户未注册");
        }
        RespUserInfoBase respUserInfoBase = this.covertRespUserInfoBase(tbUser);
        logger.info("结束查询用户信息：{}",respUserInfoBase.toString());
        return APIResponse.returnSuccess(respUserInfoBase);
    }

    /**
     * 通过微信查询用户基本信息
     * @param wxOpenId
     * @return
     */
    public APIResponse<RespUserInfoBase> queryUserInfoByWxOpenId(String wxOpenId) {
        if(wxOpenId == null){
            logger.info("wxOpenId参数不能为空！");
            return APIResponse.returnFail("wxOpenId不能为空！");
        }
        logger.info("开始根据微信查询用户信息:{}",wxOpenId);
        TbUser tbUser = this.getUser(wxOpenId, this.wxType);
        if(tbUser == null){
            logger.info("用户未注册!");
            return APIResponse.returnFail("用户未注册");
        }
        RespUserInfoBase respUserInfoBase = this.covertRespUserInfoBase(tbUser);
        logger.info("结束查询用户信息：{}",respUserInfoBase.toString());
        return APIResponse.returnSuccess(respUserInfoBase);
    }


    /**
     * 批量查询用户基本信息
     * @param lstUserId
     * @return
     */
    public APIResponse<List<RespUserInfoBase>> batchQueryUserInfo(List<Integer> lstUserId) {
        if(lstUserId == null || lstUserId.size() < 1){
            logger.info("参数lstUserId不能为空!");
            return APIResponse.returnFail("参数不能为空！");
        }
        List<TbUser> lstTbUser = this.batchQueryTbUser(lstUserId);
        if(lstTbUser.size() < 1){
            logger.info("未查到用户信息！");
            return APIResponse.returnFail("批量未查到用户信息");
        }
        List<RespUserInfoBase> result = new ArrayList<RespUserInfoBase>();
        for (TbUser tbUser : lstTbUser){
            result.add(this.covertRespUserInfoBase(tbUser));
        }
        return APIResponse.returnSuccess(result);
    }


    /**
     * 用户查看好友列表信息
     * @param uid
     * @param lstUserId //好友uid
     * @return
     */
    public APIResponse<List<RespUserInfoBase>> batchQueryUserInfo(Integer uid , List<Integer> lstUserId) {
        if(uid == null){
            logger.info("uid不能为空");
            return APIResponse.returnFail("uid 不能为空！");
        }

        if(lstUserId == null || lstUserId.size() < 1){
            logger.info("参数lstUserId不能为空!");
            return APIResponse.returnFail("参数不能为空！");
        }

        List<TbUser> lstTbUser = this.batchQueryTbUser(lstUserId);
        if(lstTbUser.size() < 1){
            logger.info("未查到用户信息！");
            return APIResponse.returnFail("批量未查到用户信息");
        }
        List<RespUserInfoBase> result = new ArrayList<RespUserInfoBase>();
        for (TbUser tbUser : lstTbUser){
            result.add(this.covertRespUserInfoBase(tbUser));
        }

        /** <<=============== 查询好友备注====================>>**/
        List<TbUserFollow> lstUf = this.getUserFriendRemark(uid,lstUserId);
        if(lstUf != null && lstUf.size() > 0){
            for(RespUserInfoBase r: result){
                for (TbUserFollow f : lstUf){
                    if(r.getUid().intValue() == f.getFollowId().intValue()){
                        if(f.getComment() == null)
                            r.setFriendRemarks("");
                        else
                            r.setFriendRemarks(f.getComment());
                    }
                }
            }
        }
        return APIResponse.returnSuccess(result);
    }

    /**
     * 批量查询用户详细信息
     * @param lstUserId
     * @return
     */
    public APIResponse<List<RespUserInfoDetail>> batchQueryUserInfoDetail(List<Integer> lstUserId) {
        if(lstUserId == null || lstUserId.size() < 1){
            logger.info("参数lstUserId不能为空!");
            return APIResponse.returnFail("参数不能为空！");
        }
        List<RespUserInfoDetail> result = this.batchQueryUserDetail(lstUserId);
        if(result.size() < 1){
            logger.info("未查到用户信息！");
            return APIResponse.returnFail("批量未查到用户信息");
        }
        return APIResponse.returnSuccess(result);
    }


    /**
     * 批量查询好友用户详细信息
     * @param lstUserId
     * @return
     */
    public APIResponse<List<RespUserInfoDetail>> batchQueryUserInfoDetail(Integer uid,List<Integer> lstUserId) {
        if(lstUserId == null || lstUserId.size() < 1){
            logger.info("参数lstUserId不能为空!");
            return APIResponse.returnFail("参数不能为空！");
        }
        List<RespUserInfoDetail> result = this.batchQueryUserDetail(lstUserId);
        if(result.size() < 1){
            logger.info("未查到用户信息！");
            return APIResponse.returnFail("批量未查到用户信息");
        }
        /** <<=============== 查询好友备注====================>>**/
        List<TbUserFollow> lstUf = this.getUserFriendRemark(uid,lstUserId);
        if(lstUf != null && lstUf.size() > 0){
            for(RespUserInfoDetail  r: result){
                for (TbUserFollow f : lstUf){
                    if(r.getUid().intValue() == f.getFollowId()){
                        if(f.getComment() == null)
                            r.setFriendRemarks("");
                        else
                            r.setFriendRemarks(f.getComment());                    }
                }
            }
        }

        return APIResponse.returnSuccess(result);
    }


    /**
     * 批量查询用户详细信息
     * @param lstUserId
     * @return
     */
    public Map<Integer, RespUserInfoBase> batchQueryUserInfoToMap(List<Integer> lstUserId) {
        if(lstUserId == null || lstUserId.size() < 1){
            logger.info("参数lstUserId不能为空!");
            return null;
        }
        List<RespUserInfoDetail> result = this.batchQueryUserDetail(lstUserId);
        if(result.size() < 1){
            logger.info("未查到用户信息！");
            return null;
        }
        Map<Integer, RespUserInfoBase> map = new HashMap<>();
        for (RespUserInfoDetail respUserInfoDetail : result){
            RespUserInfoBase respUserInfoBase = new RespUserInfoBase();
            respUserInfoBase.setUid(respUserInfoDetail.getUid());
            respUserInfoBase.setLevel(respUserInfoDetail.getLevelDe());
            respUserInfoBase.setGender(respUserInfoDetail.getGender());
            respUserInfoBase.setHeight(respUserInfoDetail.getHeightDe());
            respUserInfoBase.setWeight(respUserInfoDetail.getWeightDe());
            respUserInfoBase.setNickName(respUserInfoDetail.getNickName());
            respUserInfoBase.setAge(respUserInfoDetail.getAge());
            respUserInfoBase.setMobile(respUserInfoDetail.getMobile());
            respUserInfoBase.setAccount(respUserInfoDetail.getAccount());
            respUserInfoBase.setIcon(respUserInfoDetail.getIcon());
            respUserInfoBase.setAreaId(respUserInfoDetail.getAreaId());
            respUserInfoBase.setRealName(respUserInfoDetail.getRealName());
            respUserInfoBase.setNameVerifyStatus(respUserInfoDetail.getNameVerifyStatusDe());
            respUserInfoBase.setProvinceId(respUserInfoDetail.getProvinceId());

            map.put(respUserInfoDetail.getUid(),respUserInfoBase);
        }
        logger.info("结束批量查询用户接口转map{}",map.toString());
        return map;
    }

    /**
     * 查询用户基本信息
     * @param uid
     * @return
     */
    public TbUser getUser(Integer uid){
        String userKey = UserConstants.getUserRedisKey(uid);
        TbUser tbUser = (TbUser) myRedisTemplate.get(userKey);//查询缓存中的用户信息
        if (tbUser == null) { //判断redis是否缓存
            tbUser = tbUserMapper.selectByPrimaryKey(uid);
            if (tbUser == null)//未注册
                return null;

            //缓存用户信息并设置过期日期
            myRedisTemplate.set(userKey, tbUser,7);

            String mobile = tbUser.getMobile();
            if (StringUtils.isNotBlank(mobile)) {
                RedisKey mobileKey = new RedisKey(UserConstants.FAMILY_MOBILE, mobile);//缓存手机号与uid的映射
                myRedisTemplate.set(mobileKey.getRedisKey(),uid,7);
            }
            String weixinAppOpenid = tbUser.getWeixinAppOpenid();
            if(StringUtils.isNotBlank(weixinAppOpenid)){
                RedisKey wxKey = new RedisKey(UserConstants.FAMILY_WX_OPENID, tbUser.getWeixinAppOpenid());//缓存微信openId与uid的映射
                myRedisTemplate.set(wxKey.getRedisKey(), uid, 7);
            }
        }
        //为用户添加默认头像
        if (StringUtils.isBlank(tbUser.getIcon())) {
            tbUser.setIcon(userDefaultIcon);
        }
        return tbUser;
    }



    /**
     * 根据手机号，微信号，查询用户基本信息
     * @param mobileOrWxOpenId
     * @param type
     * @return
     */
    public TbUser getUser(String mobileOrWxOpenId,byte type){
        logger.info("开始查询用户信息 手机号 或者 微信号：{},{}",mobileOrWxOpenId,type);
        Integer uid ;
        TbUser tbUser = null;
        RedisKey key = null ;
        if(mobileType == type)
            key = new RedisKey(UserConstants.FAMILY_MOBILE, mobileOrWxOpenId);//缓存手机号与uid的映射
        else if(wxType == type)
            key = new RedisKey(UserConstants.FAMILY_WX_OPENID, mobileOrWxOpenId);//缓存微信openId与uid的映射

        uid = (Integer)myRedisTemplate.get(key);
        logger.info("得到的uid{}",uid);
        if(uid != null)
            return this.getUser(uid);

        TbUserExample example = new TbUserExample();
        TbUserExample.Criteria criteria = example.createCriteria();
        if(mobileType == type){
            criteria.andMobileEqualTo(mobileOrWxOpenId);
            key = new RedisKey(UserConstants.FAMILY_MOBILE, mobileOrWxOpenId);//缓存手机号与uid的映射
        }
        else if(wxType == type){
            criteria.andWeixinAppOpenidEqualTo(mobileOrWxOpenId);
            key = new RedisKey(UserConstants.FAMILY_WX_OPENID, mobileOrWxOpenId);//缓存微信openId与uid的映射
        }

        List<TbUser> lstTbUser = tbUserMapper.selectByExample(example);
        if(lstTbUser != null && lstTbUser.size() > 0){
            tbUser = lstTbUser.get(0);
            this.setTbUserRedis(tbUser.getId());
            myRedisTemplate.set(key.getRedisKey(),tbUser.getId(),7);
            //为用户添加默认头像
            if (StringUtils.isBlank(tbUser.getIcon())) {
                tbUser.setIcon(userDefaultIcon);
            }
        }
        return tbUser;
    }

    /**
     * 获取用户详细信息
     * @param uid
     * @return
     */
    public RespUserInfoDetail getUserDetail(Integer uid){
        String key = UserConstants.getUserDetailRedisKey(uid);//用户详细信息key
        RespUserInfoDetail respUserInfoDetail = (RespUserInfoDetail)myRedisTemplate.get(key);
        if(respUserInfoDetail == null){
            respUserInfoDetail = userInfoMapper.findUserInfoDetail(uid);

            if(respUserInfoDetail == null) {
                return null;
            }
            myRedisTemplate.set(key, respUserInfoDetail, 7);

            RedisKey mobileKey = new RedisKey(UserConstants.FAMILY_MOBILE, respUserInfoDetail.getMobile());//缓存手机号与uid的映射
            myRedisTemplate.set(mobileKey.getRedisKey(), uid, 7);

        }
        if (respUserInfoDetail.getYear() != null) {
            respUserInfoDetail.setAge(this.getCurrentAge(respUserInfoDetail.getYear()));
        }
        //为用户添加默认头像
        if (StringUtils.isBlank(respUserInfoDetail.getIcon())) {
            respUserInfoDetail.setIcon(userDefaultIcon);
        }
        return respUserInfoDetail;
    }

    /**
     * 批量获取用户基本信息
     * @param lstUid
     * @return
     */
    public List<TbUser> batchQueryTbUser(List<Integer> lstUid){
        logger.info("开始批量查询用户基本信息：size:{},{}",lstUid.size(),lstUid.toString());
        List<TbUser> result = new ArrayList<TbUser>();

        List<Integer> lstDbUid = new ArrayList<Integer>();
        for (Integer uid : lstUid){
            String key = UserConstants.getUserRedisKey(uid);
            TbUser tbUser = (TbUser)myRedisTemplate.get(key);
            if(tbUser != null){
                //为用户添加默认头像
                if (StringUtils.isBlank(tbUser.getIcon())) {
                    tbUser.setIcon(userDefaultIcon);
                }
                result.add(tbUser);
            }
            else
                lstDbUid.add(uid);
        }

        logger.info("在缓存中的UID的数量:{}",result.size());
        if(lstDbUid.size() < 1){
            logger.info("批量查询用户基本信息结束：{}",result.toString());
            return result;
        }

        logger.info("未在缓存中的UID:{}",lstDbUid.toString());
        TbUserExample tbUserExample = new TbUserExample();
        TbUserExample.Criteria criteria = tbUserExample.createCriteria();
        criteria.andIdIn(lstDbUid);
        List<TbUser> lst = tbUserMapper.selectByExample(tbUserExample);
        for (TbUser tbUser : lst ){
            //为用户添加默认头像
            if (StringUtils.isBlank(tbUser.getIcon())) {
                tbUser.setIcon(userDefaultIcon);
            }
            result.add(tbUser);
            myRedisTemplate.set(UserConstants.getUserRedisKey(tbUser.getId()),tbUser,7);
        }
        logger.info("批量查询用户基本信息结束：{}，{}",result.size(),result.toString());
        return result;
    }

    /**
     * 批量获取用户详细信息
     * @param lstUid
     * @return
     */
    public List<RespUserInfoDetail> batchQueryUserDetail(List<Integer> lstUid){
        logger.info("开始批量查询用户详细信息：size:{},{}",lstUid.size(),lstUid.toString());
        List<RespUserInfoDetail> result = new ArrayList<RespUserInfoDetail>();

        List<Integer> lstDbUid = new ArrayList<>();
        for (Integer uid : lstUid){
            String key = UserConstants.getUserDetailRedisKey(uid);
            RespUserInfoDetail respUserInfoDetail = (RespUserInfoDetail)myRedisTemplate.get(key);
            if(respUserInfoDetail != null){
                if (respUserInfoDetail.getYear() != null) {
                    respUserInfoDetail.setAge(this.getCurrentAge(respUserInfoDetail.getYear()));
                }
                //为用户添加默认头像
                if (StringUtils.isBlank(respUserInfoDetail.getIcon())) {
                    respUserInfoDetail.setIcon(userDefaultIcon);
                }
                result.add(respUserInfoDetail);
            }
            else
                lstDbUid.add(uid);
        }

        logger.info("在缓存中的UID的数量:{}",result.size());
        if(lstDbUid.size() < 1){
            logger.info("批量查询用户基本信息结束：{}",result.toString());
            return result;
        }

        logger.info("未在缓存中的UID:{}", lstDbUid.toString());

        List<RespUserInfoDetail> lst = userInfoMapper.batchFindUserInfoDetail(lstDbUid);
        for (RespUserInfoDetail respUserInfoDetail : lst ){
            if (respUserInfoDetail.getYear() != null) {
                respUserInfoDetail.setAge(this.getCurrentAge(respUserInfoDetail.getYear()));
            }
            //为用户添加默认头像
            if (StringUtils.isBlank(respUserInfoDetail.getIcon())) {
                respUserInfoDetail.setIcon(userDefaultIcon);
            }
            result.add(respUserInfoDetail);
            myRedisTemplate.set(UserConstants.getUserDetailRedisKey(respUserInfoDetail.getUid()),respUserInfoDetail,7);
        }
        logger.info("结束批量查询用户详细信息：{}，{}", result.size(), result.toString());
        return result;
    }


    /**
     * 转换返回用户信息
     */
    public RespUserInfoBase covertRespUserInfoBase(TbUser tbUser) {
        RespUserInfoBase respUserInfoBase = new RespUserInfoBase();
        respUserInfoBase.setUid(tbUser.getId());
        respUserInfoBase.setGender(tbUser.getGender().intValue());
        respUserInfoBase.setNickName(tbUser.getNickName());
        //为用户添加默认头像
        if (StringUtils.isBlank(tbUser.getIcon())) {
            respUserInfoBase.setIcon(userDefaultIcon);
        }
        respUserInfoBase.setIcon(tbUser.getIcon());
        respUserInfoBase.setRealName(tbUser.getName());
        respUserInfoBase.setMobile(tbUser.getMobile());
        if(tbUser.getYear() != null)
            respUserInfoBase.setAge(this.getCurrentAge(tbUser.getYear().intValue()));
        respUserInfoBase.setAccount(tbUser.getAccount());
        respUserInfoBase.setAreaId(tbUser.getAreaId());
        respUserInfoBase.setProvinceId(tbUser.getProvinceId());
        respUserInfoBase.setRegTime(tbUser.getRegTime());
        return respUserInfoBase;
    }

    /**
     * 获取年龄
     */
    @Override
    public int getCurrentAge(int year) {
        if (year == 0)
            return 0;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        Date date = new Date();
        Integer currentYear = Integer.parseInt(sdf.format(date));
        return currentYear - year;
    }

    /**
     * 设置缓存
     * @param uid
     */
//    @Async
    public void setTbUserRedis(Integer uid){
        String key = UserConstants.getUserRedisKey(uid);
        String detailKey = UserConstants.getUserDetailRedisKey(uid);
        TbUser tbUser = tbUserMapper.selectByPrimaryKey(uid);
        logger.info("更新的用户数据为：{}，用户id为：{}",tbUser,uid);
        myRedisTemplate.set(key, tbUser, 7);
        RespUserInfoDetail respUserInfoDetail = userInfoMapper.findUserInfoDetail(uid);
        if (tbUser.getYear() == null) {
            respUserInfoDetail.setAge(0);
        } else {
            respUserInfoDetail.setAge(this.getCurrentAge(tbUser.getYear()));
        }
        logger.info("更新的用户详情为：{},用户ID为{}，时间为：{}",respUserInfoDetail,uid, DateUtils.getStandardCurrentTime());
        myRedisTemplate.set(detailKey, respUserInfoDetail, 7);

    }

    /**
     * 查询好友备注
     * @param uid //
     * @param friendUid //好友uid
     * @return
     */
    public String getUserFriendRemark(Integer uid , Integer friendUid){
        String key = UserConstants.getFriendRemark(uid,friendUid);
        TbUserFollow tbUserFollow = (TbUserFollow)myRedisTemplate.get(key);
        if(tbUserFollow == null){
            TbUserFollowExample tbUserFollowExample = new TbUserFollowExample();
            TbUserFollowExample.Criteria criteria = tbUserFollowExample.createCriteria();
            criteria.andUidEqualTo(uid);
            criteria.andFollowIdEqualTo(friendUid);
            List<TbUserFollow> lst = tbUserFollowMapper.selectByExample(tbUserFollowExample);
            if(lst != null && lst.size() > 0){
                tbUserFollow = lst.get(0);
                myRedisTemplate.set(key,tbUserFollow ,7);
            }
        }
        if(tbUserFollow == null)
            return "";
        return tbUserFollow.getComment() == null ? "" : tbUserFollow.getComment(); // 好友备注
    }


    /**
     * 查询好友备注
     * @param uid
     * @param lstFriendUid 好友列表
     * @return
     */
    public List<TbUserFollow> getUserFriendRemark(Integer uid , List<Integer> lstFriendUid){
        logger.info("查找{}的好友备注{}",uid,lstFriendUid);
        List<TbUserFollow> result = new ArrayList<>();
        List<Integer> dbUid = new ArrayList<>();
        for (Integer friendUid : lstFriendUid ){
            String key = UserConstants.getFriendRemark(uid,friendUid);
            TbUserFollow tbUserFollow = (TbUserFollow)myRedisTemplate.get(key);
            if(tbUserFollow !=null )
                result.add(tbUserFollow);
            else
                dbUid.add(friendUid);
        }

        if(dbUid.size() < 1){
            return result;
        }

        TbUserFollowExample tbUserFollowExample = new TbUserFollowExample();
        TbUserFollowExample.Criteria criteria = tbUserFollowExample.createCriteria();
        criteria.andUidEqualTo(uid);
        criteria.andFollowIdIn(dbUid);
        List<TbUserFollow> lst = tbUserFollowMapper.selectByExample(tbUserFollowExample);
        if(lst != null && lst.size() > 0){
            for (TbUserFollow tbUserFollow :lst){
                String key = UserConstants.getFriendRemark(uid,tbUserFollow.getFollowId());
                myRedisTemplate.set(key,tbUserFollow , 7);
                result.add(tbUserFollow);
            }
        }

        logger.info("查询好友列表结束：{}",result.toString());
        return  result;
    }

    @Override
    public void setFriendRemark(Integer uid ,Integer followId){
        TbUserFollowExample tbUserFollowExample = new TbUserFollowExample();
        TbUserFollowExample.Criteria criteria = tbUserFollowExample.createCriteria();
        criteria.andUidEqualTo(uid);
        criteria.andFollowIdEqualTo(followId);
        List<TbUserFollow> lst = tbUserFollowMapper.selectByExample(tbUserFollowExample);
        if(lst != null && lst.size() > 0){
            String key = UserConstants.getFriendRemark(uid,followId);
            myRedisTemplate.set(key,lst.get(0),7);
        }

    }


    /**
     * 批量获取用户基本信息
     * @param lstUid
     * @return
     */
    public Map<Integer,TbUser> batchQueryTbUserMap(List<Integer> lstUid){
        List<TbUser> lstTbUser = this.batchQueryTbUser(lstUid);
        Map<Integer ,TbUser> m = new HashMap<>();
        if (lstTbUser != null && lstTbUser.size() > 0)
            m =  Maps.uniqueIndex(lstTbUser, this.listTbUserToMap());
        return m;
    }


    public Function<TbUser, Integer> listTbUserToMap() {
        return new Function<TbUser, Integer>() {
            @Override
            public Integer apply(TbUser input) {
                return input.getId();
            }
        };
    }


}
